import $store from '@/store/index.js';
export default {
	/* 获取黑名单列表 */
	getFrindBlackList() {
		return new Promise((resolve, reject) => {
			// 然后执行join操作来联查friends与userinfo表
			$store.state.user.sqlite.blacks
				.fields('blacks.black_id, blacks.create_time,userinfo.friend_remark,userinfo.nick_name, userinfo.avatar_url')
				.join('userinfo', 'black_id', 'user_id')
				.get() // 执行查询
				.then(results => {
					resolve(results)
				})
				.catch(error => {
					reject(error)
				});
		})
	},
	//更新或添加黑名单
	async addOrEditBlackInfo(data) {
		//逻辑是先插入如果插入失败，就更新
		try{
			await $store.state.user.sqlite.blacks.create(data)
			return true
		}catch(e){
			try{
				await this.editBlackInfo(data.black_id,data)
				return true
			}catch(e){
				console.log(e);
				throw e
			}
		}
	},
	//获取黑名单
	getBlackInfo(black_id) {
		return new Promise((resolve, reject) => {
			$store.state.user.sqlite.blacks.where({ field: 'black_id', val: `'${black_id}'` }).first()
				.then(results => {
					resolve(results)
				})
				.catch(error => {
					reject(error)
				});
		})
	},
	//更新黑名单
	editBlackInfo(black_id,data) {
		return $store.state.user.sqlite.blacks.where({ field: 'black_id', val: `'${black_id}'`}).update(data)
	},
	//添加
	addBlackInfo(data) {
		return $store.state.user.sqlite.blacks.create(data)
	},
	//删除
	delBlackInfo(black_id) {
		return $store.state.user.sqlite.blacks.where({ field: 'black_id', val: `'${black_id}'`}).delete()
	}
}